package com.leochan.utils;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import android.content.Context;

public class StringUtils {

	public static String joinMapKeyAndValue(String sep, Map map) {
		StringBuffer fullStr = new StringBuffer();
		Iterator<Entry<String, Object>> it = map.entrySet().iterator();
		int count = 0;
		int size = map.keySet().size();
		while (it.hasNext()) {
			Entry<String, Object> e = it.next();
			if (e.getValue() != null) {
				fullStr.append(e.getKey().toString());
				fullStr.append("=");
				fullStr.append(e.getValue().toString());
				count++;
				if (count < size)
					fullStr.append("&");
			}
		}

		return fullStr.toString();
	}

	public static String joinStr(String sep, String... strs) {
		if (strs == null || strs.length == 0) {
			return "";
		}
		StringBuffer sb = new StringBuffer();
		sb.append(strs[0]);
		for (int i = 1; i < strs.length; i++) {
			sb.append(sep);
			sb.append(strs[i]);
		}

		return sb.toString();
	}

	public static String getStrByContext(Context context, int id) {
		if (context == null)
			return null;
		return context.getResources().getString(id);
	}

	public static long getLong(String str) {
		long res = -1;
		try {
			res = Long.parseLong(str);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return res;
	}

	public static double getDouble(String str) {
		double res = -1;
		try {
			res = Double.parseDouble(str);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return res;
	}

	public static int getInt(String str) {
		int res = -1;
		try {
			res = Integer.parseInt(str);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return res;
	}

}
